package com.yayo.mangement.controller.tool;

import com.yayo.mangement.authentication.context.UserContext;
import com.yayo.mangement.facade.GeneraterFacade;
import com.yayo.mangement.info.GeneraterRequest;
import com.yayo.mangement.info.UserInfo;
import com.yayo.mangement.model.Table;
import com.yayo.mangement.model.TableField;
import com.yayo.mangement.paging.Paging;
import com.yayo.mangement.response.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/sys/table")
@Api(tags = "自动生成代码")
public class GeneraterController {

    @Autowired
    private GeneraterFacade generaterFacade;

    @GetMapping("/paging")
    @ApiOperation(value = "分页查询数据库表信息")

    public Paging<Table> paging(@ApiParam(name = "pageNo", value = "页码", example = "1") @RequestParam(name = "pageNo",required = false) Integer pageNo,
                                @ApiParam(name = "pageSize", value = "页长", example = "10") @RequestParam(name = "pageSize",required = false) Integer pageSize,
                                @ApiParam(name = "tableName", value = "表名") @RequestParam(name = "tableName",required = false) String tableName){
        return generaterFacade.paging(pageNo,pageSize,tableName);
    }

    @GetMapping("/detail")
    @ApiOperation(value = "表详情信息")
    @ApiParam(name = "tableName", value = "表名")
    public Response<List<TableField>> tableDetail(@ApiParam(name = "tableName", value = "表名") @RequestParam(name = "tableName",required = true) String tableName){
        return generaterFacade.tableDetail(tableName);
    }

    @PostMapping("/generate")
    @ApiOperation(value = "自动生成")
    public Response generater(@RequestBody GeneraterRequest generaterRequest){
        UserInfo userInfo =  UserContext.getUser();
        return generaterFacade.generater(generaterRequest,userInfo.getUserName());
    }

}
